package com.github.jspxnet.worktask.model.dto;

import com.github.jspxnet.enums.YesNoEnumType;
import com.github.jspxnet.json.JsonIgnore;
import com.github.jspxnet.sober.annotation.Column;
import com.github.jspxnet.utils.BeanUtil;
import com.github.jspxnet.worktask.model.enums.FlowNodeEnumType;
import com.github.jspxnet.worktask.table.ConfFlowNode;
import com.github.jspxnet.worktask.table.WorkTask;
import lombok.Data;

import java.io.Serializable;

@Data
public class ProcessResponse<T> implements Serializable {
    @Column(caption = "是否成功",enumType = YesNoEnumType.class)
    private int success = 0;  //当发生错误的时候code才有明细

    @Column(caption = "描述")
    @JsonIgnore(isNull = true)
    private String message = null; // 请求状态描述，调试用

    @Column(caption = "流程状态")
    @JsonIgnore(isNull = true)
    private WorkTaskDto workTask;

    @Column(caption = "流程节点")
    @JsonIgnore(isNull = true)
    private ConfFlowNodeDto flowNode;


    public ProcessResponse(int success, WorkTask task)
    {
        this.success = success;
        this.workTask = BeanUtil.copy(task,WorkTaskDto.class);
        this.workTask.setFinished(task.getFlowNodeType()== FlowNodeEnumType.END.getValue()?YesNoEnumType.YES.getValue():YesNoEnumType.NO.getValue());
    }

    public ProcessResponse(int success, String message)
    {
        this.success = success;
        this.message = message;
    }

    public static <T> ProcessResponse<T> success(WorkTask workTask) {
        return new ProcessResponse<T>(YesNoEnumType.YES.getValue(), workTask);
    }

    public static <T> ProcessResponse<T> error(String message) {
        return new ProcessResponse<T>(YesNoEnumType.NO.getValue(), message);
    }

}
